package com.stephenone.handlerdemo;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    private static final String TAG = SecondActivity.class.getSimpleName();
    private Handler handler;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        new Thread(new Runnable() {
            @Override
            public void run() {
                //为调用prepare方法的线程保存一个Looper对象。
                Looper.prepare();
                //handler得到一个当前线程的Looper,得到Looper的MessageQueue对象
                handler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
                };
                //死循环将MessageQueue里面的Message对象交给handler处理
                Looper.loop();
            }
        }).start();
        //将这个Message进行入队操作
        handler.sendEmptyMessage(0403);

        HandlerThread handlerThread = new HandlerThread("test");
        handlerThread.start();
        Handler handler2 = new Handler(handlerThread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                Log.e(TAG, "handleMessage: " + Thread.currentThread());
            }
        };
        handler2.sendEmptyMessage(0232);

        textView.post(new Runnable() {
            @Override
            public void run() {

            }
        });
    }


}
